package com.example.controller;

import com.example.domain.Record;
import com.example.domain.User;
import com.example.entity.PageResult;
import com.example.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = {"/record"})
public class RecordController {


    @Autowired
    private RecordService recordService;

    @RequestMapping(value = {"/searchRecords"})
    public ModelAndView searchRecords(Record record, HttpServletRequest request,
                                      Integer pageNum, Integer pageSize) {


        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        User userSession = (User) request.getSession().getAttribute("USER_SESSION");

        ModelAndView modelAndView = new ModelAndView("record");

        PageResult pageResult =
                recordService.searchRecords(record, userSession, pageNum, pageSize);

        modelAndView.addObject("search", record);   // 数据回显
        modelAndView.addObject("pageResult", pageResult);   //  分页信息
        modelAndView.addObject("pageNum", pageNum); //  当前页码
        modelAndView.addObject("gourl", request.getRequestURI());   // 地址

        return modelAndView;

    }


}
